// pages/Index/cashOutHistory/cashOutHistory.js
const controller = require("../../controllers/Controller").controller
const controllerOrder = require("../../controllers/controllerOrder").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cashOutList: [{
      label: '今日新核销',
      value: 2,
      unit: '',
      color: 'red'
      // },{
      //   label:'今日核销金额',
      //   value:200,
      //   unit:'￥',
      //   color:'red'
    }, {
      label: '核销总数',
      value: 1684,
      unit: '',
      color: ''
      // },{
      //   label:'核销总额',
      //   value:'1684.00',
      //   unit:'¥',
      //   color:''
    }],
    startTime: '',
    endTime: '',
    orderTypeList: ['商品', '套餐卡', '优惠券', '店铺爆款', '超值抵用券'],
    selectIndex: 0,
    pageNumber: 1,
    lastPage: false,
    type: 1,
    list: [],
    shopImg: "",
    state: 8
  },

  changeOrderType(e) {
    let index = e.currentTarget.dataset.index
    let type = null
    switch (index) {
      case 0:
        type = 1;
        break;
      case 1:
        type = 2;
        break;
      case 2:
        type = 0;
        break;
      case 3:
        type = 2
        break;
      case 4:
        type = 4
        break;
    }
    this.setData({
      type,
      pageNumber: 1,
      selectIndex: index
    })
    if (index == 0) {
      this.orderList()
    } else if (index == 1) {
      this.vipDeductionRecord()
    } else {
      this.getCashOutCoupon()
    }
  },
  startTimeChange(e) {
    this.setData({
      startTime: e.detail.value
    })
  },

  endTimeChange(e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  //获取核销商圈活动列表
  getCashOutCoupon() {
    let value = {}
    value.pageNumber = this.data.pageNumber
    value.type = this.data.type
    value.startTime = this.data.startTime
    value.endTime = this.data.endTime
    value.shopId = wx.getStorageSync('shopId')
    controller.writeOffHistory(value).then(res => {
      this.setData({
        list: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },
  // 套餐卡
  vipDeductionRecord() {
    let value = {}
    value.pageNumber = this.data.pageNumber
    value.type = this.data.type
    value.startTime = this.data.startTime
    value.endTime = this.data.endTime
    value.shopId = wx.getStorageSync('shopId')
    controller.vipDeductionRecord(value).then(res => {
      this.setData({
        list: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  // 商品
  orderList(isFirst = true) {
    if (isFirst) {
      this.setData({
        lastPage: false,
        pageNumber: 1,
        list: []
      })
    }
    let pageNumber = this.data.pageNumber
    let value = {}
    value.pageNumber = pageNumber
    value.type = this.data.type
    value.state = this.data.state
    value.startTime = this.data.startTime
    value.endTime = this.data.endTime
    value.shopId = wx.getStorageSync('shopId')
    controllerOrder.orderList(value).then(res => {
      let list = this.data.list
      list = list.concat(res.data.list)
      pageNumber += 1
      this.setData({
        list,
        pageNumber,
        lastPage: res.data.lastPage
      })

    })
  },

  getMore() {
    let value = {}
    if (this.data.lastPage == false) {
      if (this.data.selectIndex == 0) {
        this.orderList(false)
      } else if (this.data.selectIndex == 1) {
        let pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: pageNumber
        })
        value.pageNumber = pageNumber
        value.type = this.data.type
        value.startTime = this.data.startTime
        value.endTime = this.data.endTime
        value.shopId = wx.getStorageSync('shopId')
        controller.vipDeductionRecord(value).then(res => {
          let list = this.data.list
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            list: list3,
            lastPage: res.data.lastPage
          })
        })
      } else {
        let pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: pageNumber
        })
        value.pageNumber = this.data.pageNumber
        value.type = this.data.type
        value.startTime = this.data.startTime
        value.endTime = this.data.endTime
        value.shopId = wx.getStorageSync('shopId')
        controller.writeOffHistory(value).then(res => {
          let list = this.data.list
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            list: list3,
            lastPage: res.data.lastPage
          })
        })
      }
    }
  },

  writeOffStatistics() {
    let value = {}
    value.shopId = wx.getStorageSync('shopId')
    controller.writeOffStatistics(value).then(res => {
      let cashOutList = this.data.cashOutList
      cashOutList[0].value = res.data.writeOffToDayNum
      cashOutList[1].value = res.data.writeOffTotalNum
      this.setData({
        cashOutList
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopImg: wx.getStorageSync('shopImg')
    })

  },

  onShow: function () {
    this.orderList()
    this.writeOffStatistics()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

})